/*
 * @Author: wuqingchun
 * @Date: 2024-09-24 14:33:34
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-10-03 15:34:33
 */
#ifndef __CHAT_SESSION_H__
#define __CHAT_SESSION_H__

#include <cstdint>
#include <condition_variable>
#include <mutex>
#include <atomic>
#include <thread>

#include "chat_net.h"
#include "chat_user.h"

typedef enum tagChatSessionCommand
{
    CHAT_SESSION_CMD_HELP,
    CHAT_SESSION_CMD_CHAT,
    CHAT_SESSION_CMD_ADD_FRIEND,
    CHAT_SESSION_CMD_CREATE_GROUP,
    CHAT_SESSION_CMD_JOIN_GROUP,
    CHAT_SESSION_CMD_EXIT_GROUP,
    CHAT_SESSION_CMD_LOGOUT,
    CHAT_SESSION_CMD_MAX,
}CHAT_SESSION_COMMAND_E;

class CChatSession
{
public:
    CChatSession(const char* pcServerHost, uint16_t usPort);
    /**
     * @brief 用户注册
     * 
     * @param pcName 
     * @param pcPassword 
     * @return int32_t 
     */
    int32_t RegisterUser(const char* pcName, const char* pcPassword);
    int32_t Login(const char* pcName, const char* pcPassword);
    int32_t Logout(const char* pcName);
private:
    void __OnMessage(const TcpConnectionPtr& refConnPtr, Buffer* pobjBuf, Timestamp objTS);

    void __MainMenu();
    void __WaitInput(const char* pcPrompt, char* pcBuff, size_t ulBufLen);
    CHAT_SESSION_COMMAND_E __GetCommand();

    void __ChatWithFriend();
    void __AddFriend();
    void __CreateGroup();
    void __JoinGroup();
    void __ExitGroup();
    void __Logout();
private:
    CChatNet m_objNet;
    std::condition_variable m_objCV;
    std::mutex m_objMtx;
    std::atomic<bool> m_bLogin{false};
    CChatUser m_objLoginUser;

    std::array<std::function<void ()>, CHAT_SESSION_CMD_MAX> m_arrCmdHandler;
};

#endif //__CHAT_SESSION_H__